<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 放置结构内容 -->
         <tree-tools :tree-node="company" :is-root="true" />
       <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
         <tree-tools
          @addDepts='addDepts'
          slot-scope="{ data }"
          :tree-node="data" 
          @delDepts='getDepartments'
          @editDepts="editDepts"
          />
       </el-tree>
      </el-card>
    <add-dept
     ref="addDept"
     :show-dialog.sync="showDialog"
     :tree-node="node"
     @addDepts="getDepartments"
     />
    </div>
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import { _getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import AddDept from './components/add-dept.vue'
export default {
  data() {
    return {
      company: {}, // 就是头部的数据结构
      departs: [],
      defaultProps: {
        label: 'name' // 表示 从这个属性显示内容
      },
      showDialog: false, // 显示窗体
      node: null, // 记录当前点击的node节点
      loading: false // 当前的控制显示弹层
    }
  },
  components: {
    TreeTools,
    AddDept
  },
  methods: {
    async getDepartments() {
      this.loading = true
      const result = await _getDepartments()
      this.company = { name: result.companyName, manager: '负责人' }
      this.departs = tranListToTreeData(result.depts, '')
      this.loading = false
    },
    addDepts(node) {
      this.node = node
      this.showDialog = true
      console.log(node)
    },
    editDepts(node) {
      this.showDialog = true
      this.node = node // 赋值操作的节点
      this.$refs.addDept.getDepartDetail(node.id)
    }
  },
  created() {
    this.getDepartments() // 调用自身的方法
  }
}
</script>

<style lang='scss' scopde>

</style>